package com.anosine.gpsl.android.impl;

import com.anosine.gpsl.android.AppConstants;
import com.anosine.gpsl.android.WebConstants;
import com.anosine.gpsl.android.dao.RegisterDao;
import com.anosine.gpsl.android.entity.Login;
import com.anosine.gpsl.android.presenter.BasePresenterActivity.OnLoadListener;
import com.anosine.gpsl.android.util.DeviceUtil;
import com.anosine.gpsl.android.util.HttpRequestListener;
import com.anosine.gpsl.android.util.SendHttpUtils;
import com.anosine.gpsl.android.util.ToastHelper;
import com.google.gson.reflect.TypeToken;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.lidroid.xutils.util.LogUtils;

/**
 * @Title: RegisterDaoImpl
 * @Package com.anosine.gpsl.android.impl
 * @Description: 接口实现
 * @author: sunny
 * @date: 2015-11-19
 * @version V1.0
 */

public class RegisterDaoImpl implements RegisterDao {
	private OnLoadListener loadListener;

	/**
	 * @Title: RegisterDaoImpl(OnLoadListener loadListener)
	 * @Description: 构造方法，初始化对象，给loadListener赋值
	 * @param: loadListener
	 * @throws
	 */

	public RegisterDaoImpl(OnLoadListener loadListener) {
		super();
		this.loadListener = loadListener;
	}

	/**
	 * @Title: getRegister(String username,String userpass,String carNo, String
	 *         realname,String idno,String phoneCardNo)
	 * @Description: 调用注册接口
	 * @param username
	 *            用户名
	 * @param userpass
	 *            密码
	 * @param realname
	 *            真实姓名
	 * @param idno
	 *            身份证后8位
	 * @param deviceno
	 *            设备号
	 * @param mobiphone
	 *            手机号码
	 * @param IMEI
	 *            IMEI号
	 * @return: 无
	 * @throws
	 */

	@Override
	public void getRegister(String username, String userpass, String carNo,
			String realname, String idno, String phoneCardNo) {
		RequestParams params = new RequestParams();
		params.addBodyParameter("mobile.mobilenumber", username);
		params.addBodyParameter("userinfo.userpass", userpass);
		params.addBodyParameter("mobile.realname", realname);
		params.addBodyParameter("mobile.idno", idno);
		params.addBodyParameter("mobile.deviceno", carNo);
		params.addBodyParameter("userinfo.sim", phoneCardNo);
		params.addBodyParameter("userinfo.IMEI", DeviceUtil.getDeviceImei());
		SendHttpUtils sendHttp = new SendHttpUtils();
		TypeToken type = new TypeToken<Login>() {
		};
		String url = String.format("%1s/%2s", AppConstants.sCommonUrl,
				WebConstants.Do_Regist_Action);
		sendHttp.doRequest(url, params, type,
		/**
		 * @Title: onSuccess(String result, Login t)
		 * @Description: 解析成功做的操作
		 * @param: result json
		 * @param: t 注册的实体类
		 * @return: 无
		 * @throws
		 */
		new HttpRequestListener<Login>() {
			@Override
			public void onSuccess(String result, Login t) {

				loadListener.onSuccess(t);

			}

			/**
			 * @Title: onFailure(String error)
			 * @Description: 解析失败做的操作
			 * @param: error 错误信息
			 * @return: 无
			 * @throws
			 */
			@Override
			public void onFailure(String error) {
				loadListener.onFailure();
				ToastHelper.toast(error);
			}
		});
	}

}
